from typing import *


class Solution:

    def minimumOperations(self, nums: List[int]) -> int:
        n = len(nums)
        f = [[0] * 4 for _ in range(n + 1)]
        for i in range(1, n + 1):
            for k in range(1, 4):
                cnt = nums[i - 1] != k
                f[i][k] = f[i - 1][k] + cnt
                for j in range(i - 1, 0, -1):
                    for p in range(1, k):
                        f[i][k] = min(f[i][k], f[j][p] + cnt)
                    cnt += nums[j - 1] != k
        return min(f[n][1:])
    
class Solution:

    def minimumOperations(self, nums: List[int]) -> int:
        n = len(nums)
        f = [[0] * 4 for _ in range(n + 1)]
        for i in range(1, n + 1):
            for k in range(1, 4):
                cnt = nums[i - 1] != k
                f[i][k] = f[i - 1][k] + cnt
                for p in range(1, k + 1):
                    f[i][k] = min(f[i][k], f[i - 1][p] + cnt)
        return min(f[n][1:])

s = Solution()
print(s.minimumOperations([2, 1, 3, 2, 1]))
